package cn.tansanqinger.controller.topic;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.tansanqinger.pojo.Commenttopic;
import cn.tansanqinger.pojo.Topic;
import cn.tansanqinger.pojo.User;
import cn.tansanqinger.service.comment.ICommentTopicService;
import cn.tansanqinger.service.hymenu.IHyMenuServer;
import cn.tansanqinger.service.topic.ITopicService;
import cn.tansanqinger.util.PageData;
import cn.tansanqinger.util.Tools;

@Controller
@RequestMapping("/topic/user")
public class TopicContentController {

	@Autowired
	private IHyMenuServer hyMenuServer;
	@Autowired
	private ITopicService service;
	@Autowired
	private ICommentTopicService cservice;
	
	/**
	 * 作题首页
	 * @param request
	 * @param mv
	 * @return
	 */
	@RequestMapping("/content")
	public ModelAndView content(HttpServletRequest request, ModelAndView mv) {
		mv = hyMenuServer.selectHyMenuAll(mv);
		User user = (User) request.getSession().getAttribute("user");
		Topic topic = service.randSelectTopic(request);
		mv.addObject("topic", topic);
		mv.addObject("user", user);
		mv.addObject("ttype", request.getParameter("ttype"));
		if(topic!=null) mv.addObject("collect", service.isCollectTopic(topic.getTid(), user.getId()));
		mv.setViewName("topic/content");
		return mv;
	}
	
	/**
	 * 单一页面
	 * @param request
	 * @param mv
	 * @return
	 */
	@RequestMapping("/selectComment")
	public ModelAndView selectComment(HttpServletRequest request, ModelAndView mv) {
		mv = hyMenuServer.selectHyMenuAll(mv);
		User user = (User) request.getSession().getAttribute("user");
		Topic topic = service.selectTopicOne(request);
		mv.addObject("topic", topic);
		mv.addObject("user", user);
		if(topic!=null) {
			mv.addObject("collect", service.isCollectTopic(topic.getTid(), user.getId()));
			mv.addObject("ttype", topic.getTtype());
		}
		mv.setViewName("topic/selectComment");
		return mv;
	}
	
	@RequestMapping("/verify")
	@ResponseBody
	public PageData verify(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		PageData pd = service.submitTopicResult(request);
		pd.put("commentes",cservice.selectCommentMessage(user, 1, Integer.parseInt(Tools.isEmpty(request.getParameter("tid")))));
		return pd;
	}
	
	@RequestMapping("/collect")
	@ResponseBody
	public PageData collect(HttpServletRequest request) {
		return service.collectTopic(request);
	}
	
	@RequestMapping("/shield")
	@ResponseBody
	public PageData shield(HttpServletRequest request) {
		return service.shieldTopic(request);
	}
	
	/**
	 * 举报
	 * @param request
	 * @return
	 */
	@RequestMapping("/report")
	@ResponseBody
	public PageData report(HttpServletRequest request) {
		return service.reportTopic(request);
	}
	
	@RequestMapping("/mistake")
	@ResponseBody
	public PageData mistake(HttpServletRequest request) {
		return service.queryTopic(request);
	}
	
	/**
	 * 回复评论
	 * @param request
	 * @return
	 */
	@RequestMapping("/replyComment")
	@ResponseBody
	public PageData replyComment(HttpServletRequest request) {
		return cservice.replyComment(request);
	}
	
	/**
	 * 添加数据
	 * @param request
	 * @return
	 */
	@RequestMapping("/addComment")
	@ResponseBody
	public PageData addComment(HttpServletRequest request) {
		return cservice.add(request);
	}
	
	@RequestMapping("/praiseSumbit")
	@ResponseBody
	public PageData praiseSumbit(HttpServletRequest request) {
		return cservice.praiseSumbit(request);
	}
	
	@RequestMapping("/treadSumbit")
	@ResponseBody
	public PageData treadSumbit(HttpServletRequest request) {
		return cservice.treadSumbit(request);
	}
	
	@RequestMapping("/replyToLoad")
	@ResponseBody
	public List<Commenttopic> replyToLoad(HttpServletRequest request) {
		return cservice.selectCommentTwoTiterMessage(request);
	}
}
